package com.zzw.config;

import org.apache.shiro.session.Session;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * Redis Shiro配置类
 * 提供专门用于Shiro会话管理的RedisTemplate
 */
@Configuration
public class RedisShiroConfig {

    /**
     * 创建专门用于Shiro会话的RedisTemplate
     * 使用名称"shiroSessionRedisTemplate"以匹配依赖注入需求
     * 
     * @param redisConnectionFactory Redis连接工厂
     * @return RedisTemplate实例
     */
    @Bean(name = "shiroSessionRedisTemplate")
    public RedisTemplate<String, Session> shiroSessionRedisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<String, Session> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(redisConnectionFactory);

        // 设置key的序列化方式
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());

        // 使用JDK序列化方式处理Shiro会话对象
        // GenericJackson2JsonRedisSerializer可能无法正确处理Shiro的Session对象
        JdkSerializationRedisSerializer jdkSerializer = new JdkSerializationRedisSerializer();
        redisTemplate.setValueSerializer(jdkSerializer);
        redisTemplate.setHashValueSerializer(jdkSerializer);

        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }
} 